<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/24
 * Time: 16:55
 */
namespace IDCU\Office;
use IDCU\Office;

class ExcelOffice extends Office
{
    protected function getExecl($filePath){
        Vendor('PHPExcel.PHPExcel', '', '.php');
        $PHPReader = new \PHPExcel_Reader_Excel2007();
        if (!$PHPReader->canRead($filePath)) {
            $PHPReader = new \PHPExcel_Reader_Excel5();
            if (!$PHPReader->canRead($filePath)) {
                echo 'no Excel';
                return;
            }
        }
        $excel = $PHPReader->load($filePath);
        return $excel;
    }

    public function getSheet($filePath,$sheetNo=0)
    {
        $excel = $this->getExecl($filePath);
        $sheet = $excel->getSheet($sheetNo);
        return $sheet;
    }

    public function getArr($filePath,$sheetNo=0,$startRow=1){
        $sheet = $this->getSheet($filePath,$sheetNo);
        $columnCount = $sheet->getHighestColumn();
        $rowCount = $sheet->getHighestRow();
        $arr = array();
        $keys = array();
        for($row=$startRow;$row<=$rowCount;$row++){
            for($column='A';$column<=$columnCount;$column++){
                $val = trim($sheet->getCellByColumnAndRow(ord($column)-65,$row)->getValue());
                if($row == $startRow){
                    $keys[$column] = $val;
                }else{
                    $data[$keys[$column]] = $val;
                }
            }
            if($row > $startRow){
                $arr[] = $data;
            }
        }
        return $arr;
    }
}